﻿using System;
using System.ComponentModel;

namespace UtfprLibrary.EntityFramework.ComponentModel {
    /// <summary>
    /// Exposes all the EntitySets in a UtfprDataSource as a list of PropertyDescriptor objects.
    /// </summary>
    /// <remarks>
    /// This is required for showing the available EntitySets in the drop down that
    /// appears when editing the "DataMember" property of a complex bound control.
    /// 
    /// Reflected from DataViewManagerListItemTypeDescriptor
    /// </remarks>
    public class EntitySetTypeDescriptor : ICustomTypeDescriptor {
        #region Fields

        IEntityDataSource _dataSource;
        PropertyDescriptorCollection _pdc;

        #endregion

        #region Methods

        public EntitySetTypeDescriptor(IEntityDataSource dataSource) {
            this._dataSource = dataSource;
        }

        internal void Reset() {
            this._pdc = null;
        }

        #endregion

        #region ICustomTypeDescriptor

        PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes) {
            if (this._pdc == null) {
                PropertyDescriptor[] properties = null;
                int count = this._dataSource.EntitySets.Count;
                properties = new PropertyDescriptor[count];
                for (int i = 0; i < count; i++) {
                    properties[i] = new EntitySetPropertyDescriptor(this._dataSource.EntitySets[i]);
                }
                this._pdc = new PropertyDescriptorCollection(properties);
            }
            return this._pdc;
        }

        object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd) { return this; }
        AttributeCollection ICustomTypeDescriptor.GetAttributes() { return new AttributeCollection(null); }
        string ICustomTypeDescriptor.GetClassName() { return null; }
        string ICustomTypeDescriptor.GetComponentName() { return null; }
        TypeConverter ICustomTypeDescriptor.GetConverter() { return null; }
        EventDescriptor ICustomTypeDescriptor.GetDefaultEvent() { return null; }
        PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty() { return null; }
        object ICustomTypeDescriptor.GetEditor(Type editorBaseType) { return null; }
        EventDescriptorCollection ICustomTypeDescriptor.GetEvents() { return new EventDescriptorCollection(null); }
        EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes) { return new EventDescriptorCollection(null); }
        PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties() { return ((ICustomTypeDescriptor)this).GetProperties(null); }

        #endregion
    }
}
